﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace msnet_2
{
    public partial class TrigleForm : Form
    {
        public TrigleForm()
        {
            InitializeComponent();
        }

        private void randomTrigleBtn_Click(object sender, EventArgs e)
        {
            int a, b, c;
            var random = new Random();
            do
            {
                a = random.Next(1, 100);
                b = random.Next(1, 100);
                c = random.Next(1, 100);

                // 检验三边是否可以构成三角形
                if ((a + b) > c && (a + c) > b && (b + c) > a)
                {
                    boxA.Text = a.ToString();
                    boxB.Text = b.ToString();
                    boxC.Text = c.ToString();
                    break;
                }
                // 不符合三边规则，递归再次生成
            } while (true);

        }

        private void calcBtn_Click(object sender, EventArgs e)
        {
            try
            {
                int a, b, c;
                int.TryParse(boxA.Text, out a);
                int.TryParse(boxB.Text, out b);
                int.TryParse(boxC.Text, out c);
                // 海伦公式求三角形面积
                var p = (a + b + c) / 2;
                var area = Math.Sqrt(p * (p - a) * (p - b) * (p - c));
                boxS.Text = area.ToString();
            }
            catch (Exception)
            {
                MessageBox.Show("边长有误，请重新输入");
            }
        }
    }
}
